/**
 * @Project Name:blog
 * @Package Name:cn.hoofungson.blog.core.text
 * @Since JDK 1.8
 */
package cn.hoofungson.blog.business.service.provider.text;

import java.text.DateFormat;
import java.text.FieldPosition;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

/**
 * @Description: 自定义日期格式化
 * @Author 胡松 hoofungson@163.com
 * @Date 2018-09-17 12:48
 * @Version V1.0
 */
public class BlogDateFormat extends DateFormat {

    private DateFormat dateFormat;

    public BlogDateFormat(DateFormat dateFormat) {
        this.dateFormat = dateFormat;
    }

    /**
     * Formats a Date into a date/time string.
     *
     * @param date          a Date to be formatted into a date/time string.
     * @param toAppendTo    the string buffer for the returning date/time string.
     * @param fieldPosition keeps track of the position of the field
     *                      within the returned string.
     *                      On input: an alignment field,
     *                      if desired. On output: the offsets of the alignment field. For
     *                      example, given a time text "1996.07.10 AD at 15:08:56 PDT",
     *                      if the given fieldPosition is DateFormat.YEAR_FIELD, the
     *                      begin index and end index of fieldPosition will be set to
     *                      0 and 4, respectively.
     *                      Notice that if the same time field appears
     *                      more than once in a pattern, the fieldPosition will be set for the first
     *                      occurrence of that time field. For instance, formatting a Date to
     *                      the time string "1 PM PDT (Pacific Daylight Time)" using the pattern
     *                      "h a z (zzzz)" and the alignment field DateFormat.TIMEZONE_FIELD,
     *                      the begin index and end index of fieldPosition will be set to
     *                      5 and 8, respectively, for the first occurrence of the timezone
     *                      pattern character 'z'.
     * @return the string buffer passed in as toAppendTo, with formatted text appended.
     */
    @Override
    public StringBuffer format(Date date, StringBuffer toAppendTo, FieldPosition fieldPosition) {
        return dateFormat.format(date, toAppendTo, fieldPosition);
    }

    /**
     * Parse a date/time string according to the given parse position.  For
     * example, a time text {@code "07/10/96 4:5 PM, PDT"} will be parsed into a {@code Date}
     * that is equivalent to {@code Date(837039900000L)}.
     *
     * <p> By default, parsing is lenient: If the input is not in the form used
     * by this object's format method but can still be parsed as a date, then
     * the parse succeeds.  Clients may insist on strict adherence to the
     * format by calling {@link #setLenient(boolean) setLenient(false)}.
     *
     * <p>This parsing operation uses the {@link #calendar} to produce
     * a {@code Date}. As a result, the {@code calendar}'s date-time
     * fields and the {@code TimeZone} value may have been
     * overwritten, depending on subclass implementations. Any {@code
     * TimeZone} value that has previously been set by a call to
     * {@link #setTimeZone(TimeZone) setTimeZone} may need
     * to be restored for further operations.
     *
     * @param source The date/time string to be parsed
     * @param pos    On input, the position at which to start parsing; on
     *               output, the position at which parsing terminated, or the
     *               start position if the parse failed.
     * @return A {@code Date}, or {@code null} if the input could not be parsed
     */
    @Override
    public Date parse(String source, ParsePosition pos) {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(source, pos);
    }

    /**
     * Overrides Cloneable
     */
    @Override
    public Object clone() {
        Object format = dateFormat.clone();
        return new BlogDateFormat((DateFormat) format);
    }
}
